package adventuregame;

import java.util.ArrayList;

/**
 * Contains the player and stores information about him.
 * @author Jesper & Simon
 */
public class Person {

	private String location = "";
	Console console = new Console();
	ArrayList<Thing> inventory = new ArrayList<Thing>();

	/**
	 * Initilizes person/gamer and set assigned Room as location.
	 * @param r Room, gamers start location.
	 */
	public Person(Room r) {
		location = r.toString();
	}

	/**
	 * Gets gamers current location.
	 * @return String gamers location
	 */
	public String getLocation() {
		return location;
	}

	/**
	 * Sets gamers location.
	 * @param r String name of location room
	 */
	public void setLocation(String r) {
		location = r;
	}

	/**
	 * Return a list of things contained by this person, or empty if there is none
	 * @param isMenu boolean, determines whether list should be shown as a comma separated(false) or as a list of options with number(true)
	 * @return String list of things contained by this Person object
	 */
	public String getInventory(boolean isMenu) {
		int max = inventory.size();
		String returnString = "";

		if (max == 0) {
			returnString = "empty";
		} else {
			returnString = inventoryList(max, isMenu);
		}
		return returnString;
	}

	/**
	 * Creates the list for getInventory().
	 * @param max int from getInventory()
	 * @param isMenu boolean from getInventory()
	 * @return String list of things contained by this Person object
	 */
	private String inventoryList(int max, boolean isMenu) {

		String returnString = "";
		for (int i = 0; i < max; i++) {
			if (isMenu) {
				returnString += "[" + i + "]" + inventory.get(i) + "\n";
			} else {
				if (i == inventory.size() - 1) {
					returnString += inventory.get(i);
				} else {
					returnString += inventory.get(i) + ", ";
				}
			}


		}
		return returnString;
	}

	/**
	 * Adds given Thing to Persons inventory, and makes sure inventory maximum contains 3 items
	 * @param object Thing to add.
	 */
	public void addToInventory(Thing object) {
		if (inventory.size() < 3) {
			inventory.add(object);
		} else {
			console.println("You can only carry 3 items");
		}
	}

	/**
	 * Gets Thing at given index in inventory ArrayList
	 * @param position Int index in ArrayList
	 * @return Thing at given index in inventory ArrayList
	 */
	public Thing getThingAt(int position) {
		return inventory.get(position);
	}

	/**
	 * Remove Thing at given index in inventory ArrayList
	 * @param position Int index in ArrayList
	 */
	public void removeThingAt(int position) {
		inventory.remove(position);
	}
}
